﻿using System;
using System.Collections.Generic;
using System.Threading;
using Resources.Scripts.Socket.MyChat.Client;
using Resources.Scripts.Socket.MyChat.Protocol;
using Resources.Scripts.Utils;
using UnityEngine;

namespace Resources.Scripts.Socket.MyChat.Manager
{
    public class ChatManager : MonoBehaviour
    {

        public enum LocalChanel
        {
            All = 0,
            World = 1,
            Private = 2,
            System = 3 
        }

        private ChatChannel[] ChannelFilter = new ChatChannel[4]{
            ChatChannel.World | ChatChannel.Private | ChatChannel.System,
            ChatChannel.World,
            ChatChannel.Private,
            ChatChannel.System
        };

        public List<ChatMessage>[] Messages = new List<ChatMessage>[4]
        {
            new List<ChatMessage>(),
            new List<ChatMessage>(),
            new List<ChatMessage>(),
            new List<ChatMessage>()
        };


        public Action<ChatMessage> OnChat { get; internal set; }

        public static ChatManager Instance;


        private void Awake()
        {
            if (Instance != null)
            {
                Destroy(gameObject);
                return;
            }
            
            Instance = this;
        }

        private void Update()
        {
            // if (NetClient.Instance.ReData.TryDequeue(out ChatMessage message))
            // {
            //     AddMessage(message);
            // }
        }

        public void AddMessage(ChatChannel channel, List<ChatMessage> message)
        {
            for(int ch = 0; ch < 4; ch++){
                if ((ChannelFilter[ch] & channel) == channel)
                {
                    Messages[ch].AddRange(message);
                }
            }
            
            // OnChat?.Invoke();
        }

        public void AddMessage(ChatMessage message)
        {
            
            OnChat.Invoke(message);
        }

        public void AddSystemMessage(string message)
        {
            Messages[3].Add(new ChatMessage()
            {
                Channel = ChatChannel.System,
                Message = message,
            });
            
            // OnChat?.Invoke();
        }

        internal void SendChat(string content)
        {
            // ChatServer.Instance.SendChat(SendChannel, content, PrivateID, PrivateName);
            // NetClient.Instance.SendMessage(new ChatMessage(SendChannel, NetClient.Instance.userId, NetClient.Instance.userName, content));
            
            NetClient.Instance.MySendMessage(MessageFormatPack.GetSerialize(content));
        }

        public ChatChannel sendChannel;
        public ChatChannel SendChannel {
            get
            {
                return sendChannel;
            }
            set
            {
                sendChannel = value;
            }
        }

        public int PrivateID { get; set; }

        public string PrivateName { get; set; }

        
        
        //  TODO：需要事件触发
        public string GetCurrentMessage()
        {
            return null;
        }
    }
}